package com.magicsoft.share.ui.exchange;

import android.app.Dialog;
import android.content.Intent;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.magicsoft.share.R;
import com.magicsoft.share.adapter.exchange.GoodsDetailAdapter;
import com.magicsoft.share.base.BaseActivity;
import com.magicsoft.share.bean.exchange.GoodsDetail;
import com.magicsoft.share.bean.exchange.GoodsSubmit;
import com.magicsoft.share.presenter.exchange.GoodsPresenter;
import com.magicsoft.share.utils.GlideImageLoader;
import com.magicsoft.share.utils.ToastUtils;
import com.magicsoft.share.view.exchange.IGoodsView;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

public class GoodsActivity extends BaseActivity<IGoodsView, GoodsPresenter> implements IGoodsView {


    @BindView(R.id.view_bottom)
    View viewBottom;
    @BindView(R.id.btn_add_cart)
    Button btnAddCart;
    @BindView(R.id.btn_buy)
    Button btnBuy;
    @BindView(R.id.layout_bottom)
    LinearLayout layoutBottom;
    @BindView(R.id.ibtn_shop_cart)
    ImageButton ibtnShopCart;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private int goodsNum = 1;
    private boolean isBuy;
    private View topView;
    private Banner banner;
    private GoodsDetailAdapter goodsDetailAdapter;
    private long goodsId;
    private TextView tv_name;
    private TextView tv_price;
    private TextView tv_stock;
    private TextView tv_name_dialog;
    private TextView tv_price_dialog;
    private TextView tv_stock_dialog;
    private ImageView imageView_dialog;
    private TextView tv_num_dialog;
    private Dialog bottomDialog;
    private GoodsDetail mGoodsDetail;

    @Override
    public GoodsPresenter initPresenter() {
        return new GoodsPresenter();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_goods;
    }

    @Override
    public void initData() {
        goodsId = getIntent().getIntExtra("goodsId", -1);
    }

    @Override
    public void initView() {

        initRV();

        initSpecDialog();

    }

    @Override
    public void requestData() {

        mPresenter.goodsDetail(goodsId);
    }


    private void initRV() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));

        topView = LayoutInflater.from(this).inflate(R.layout.top_goods, recyclerView, false);
        tv_name = ((TextView) topView.findViewById(R.id.tv_name));
        tv_price = ((TextView) topView.findViewById(R.id.tv_price));
        tv_stock = ((TextView) topView.findViewById(R.id.tv_stock));
        banner = ((Banner) topView.findViewById(R.id.banner));
        topView.findViewById(R.id.ibtn_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());

        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {

            }
        });
        //banner设置方法全部调用完毕时最后调用


        goodsDetailAdapter = new GoodsDetailAdapter(R.layout.item_img);

        goodsDetailAdapter.addHeaderView(topView);

        recyclerView.setAdapter(goodsDetailAdapter);

    }


    private void initSpecDialog() {
        bottomDialog = new Dialog(this, R.style.AlertActivity_AlertStyle);
        View contentView = LayoutInflater.from(this).inflate(R.layout.pop_spec, null);
        tv_name_dialog = ((TextView) contentView.findViewById(R.id.tv_name));
        tv_price_dialog = ((TextView) contentView.findViewById(R.id.tv_price));
        tv_stock_dialog = ((TextView) contentView.findViewById(R.id.tv_stock));
        imageView_dialog = ((ImageView) contentView.findViewById(R.id.imageView));
        tv_num_dialog = (TextView) contentView.findViewById(R.id.tv_num);

        contentView.findViewById(R.id.tv_add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                goodsNum++;
                tv_num_dialog.setText(goodsNum + "");
            }
        });
        contentView.findViewById(R.id.tv_delete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (goodsNum == 1) {
                    ToastUtils.showToast(GoodsActivity.this, "最少选择一件哦");
                } else {
                    goodsNum--;
                    tv_num_dialog.setText(goodsNum + "");
                }

            }
        });
        contentView.findViewById(R.id.btn_confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (isBuy) {
                    if (goodsNum <= mGoodsDetail.getGoods_stock()) {
                        Intent intent = new Intent(GoodsActivity.this, SettlementActivity.class);
                        GoodsSubmit goodsSubmit = new GoodsSubmit(goodsId, mGoodsDetail.getGoodsName(), mGoodsDetail.getGoodsImg(), mGoodsDetail.getPrice(), goodsNum);
                        ArrayList<GoodsSubmit> goodsSubmits = new ArrayList<>();
                        goodsSubmits.add(goodsSubmit);
                        String goodsStr = new Gson().toJson(goodsSubmits);
                        intent.putExtra("goodsStr", goodsStr);
                        intent.putExtra("isDirect", 1);
                        intent.putExtra("orderAmount", mGoodsDetail.getPrice() * goodsNum);
                        intent.putExtra("GoodsList", goodsSubmits);
                        intent.putExtra("tyoe",1);
                        startActivity(intent);
                    } else {
                        ToastUtils.showToast(GoodsActivity.this, "库存不足，请重新选择");
                    }
                } else {
                    if (goodsNum <= mGoodsDetail.getGoods_stock()) {
                        //加入购物车
                        mPresenter.joinShopCart(goodsId, goodsNum);
                    } else {
                        ToastUtils.showToast(GoodsActivity.this, "库存不足，请重新选择");
                    }


                }
            }
        });
        bottomDialog.setContentView(contentView);
        ViewGroup.LayoutParams layoutParams = contentView.getLayoutParams();
        layoutParams.width = getResources().getDisplayMetrics().widthPixels;
        contentView.setLayoutParams(layoutParams);
        bottomDialog.getWindow().setGravity(Gravity.BOTTOM);
        bottomDialog.getWindow().setWindowAnimations(R.style.AlertActivity_AlertStyle);


    }


    @OnClick({R.id.btn_add_cart, R.id.btn_buy, R.id.ibtn_shop_cart})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_add_cart:
                isBuy = false;
                bottomDialog.show();
                //  initSpecDialog();
                break;
            case R.id.btn_buy:
                isBuy = true;
                bottomDialog.show();
                //initSpecDialog();
                break;
            case R.id.ibtn_shop_cart:
                startActivity(new Intent(this, ShopCartActivity.class));
                break;
        }
    }


    @Override
    public void goodsDetailSuccess(GoodsDetail goodsDetail) {
        //设置图片集合
        mGoodsDetail = goodsDetail;

        banner.setImages(goodsDetail.getBannerImg());
        banner.start();
        goodsDetailAdapter.setNewData(goodsDetail.getExhibitionImg());
        tv_name.setText(goodsDetail.getGoodsName());
        tv_price.setText(goodsDetail.getPrice() + "");
        tv_stock.setText("库存：" + goodsDetail.getGoods_stock());

        tv_name_dialog.setText(goodsDetail.getGoodsName());
        tv_price_dialog.setText(goodsDetail.getPrice() + "");
        tv_stock_dialog.setText("库存：" + goodsDetail.getGoods_stock());
        Glide.with(this).load(goodsDetail.getGoodsImg()).into(imageView_dialog);


    }

    @Override
    public void joinShopCartSuccess() {
        ToastUtils.showToast(GoodsActivity.this, "加入购物车成功");
        bottomDialog.dismiss();
    }

    @Override
    public void goodsDetailFail() {
        finish();
    }
}
